/*
 * jlb
 */
package com.jlb.user.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.active.dao.ActiveRecordDAO;
import com.jlb.active.entity.ActiveRecord;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.UserActiveException;
import com.jlb.core.exceptions.UserJlbException;
import com.jlb.jlb.entity.TransferType;
import com.jlb.user.dao.UserActiveDAO;
import com.jlb.user.dao.UserDAO;
import com.jlb.user.entity.User;
import com.jlb.user.entity.UserActive;
import com.jlb.user.entity.UserActiveState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userActiveSV")
@Slf4j
public class UserActiveSVImpl extends BaseSVImpl<UserActive, Long> implements UserActiveSV {

    @Autowired
    private UserActiveDAO userActiveDAO;


    @Autowired
    private ActiveRecordDAO activeRecordDAO;

    @Resource
    private UserDAO userDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return userActiveDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(UserActive entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象UserActive
     *
     * @param id
     * @return UserActive
     */
    @Override
    public UserActive load(java.lang.Long id) {
        if (id == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userActiveDAO.load(param);
    }

    /**
     * 加载一个对象UserActive 通过id
     *
     * @param id
     * @return UserActive
     */
    @Override
    public UserActive loadById(java.lang.Long id) {
        if (id == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userActiveDAO.loadById(id);
    }


    /**
     * 根据主键id,oldStates 共同更新 UserActive 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, UserActiveState newState, UserActiveState... oldStates) {
        if (id == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userActiveDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 UserActive 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, UserActiveState state) {
        if (id == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userActiveDAO.updateById(id, state, new Date());
    }


    /**
     * 删除对象UserActive
     *
     * @param id
     * @return UserActive
     */
    @Override
    public void delete(java.lang.Long id) {
        if (id == null) {
            throw new UserActiveException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        userActiveDAO.delete(param);
    }


    /**
     * 查询UserActive分页
     *
     * @param userActive 对象
     * @param offset     查询开始行
     * @param limit      查询行数
     * @return List<UserActive>
     */
    @Override
    public List<UserActive> list(UserActive userActive, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (userActive != null) {
            map = JSON.parseObject(JSON.toJSONString(userActive, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userActiveDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(UserActive userActive) {
        Map<String, Object> map = null;
        if (userActive != null) {
            map = JSON.parseObject(JSON.toJSONString(userActive, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userActiveDAO.count(map);
    }

    /**
     * 查询UserActive分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<UserActive>
     */
    @Override
    public List<UserActive> list(java.lang.Long id, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userActiveDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userActiveDAO.count(param);
    }


    /**
     * 平台为用户充值或扣除激活币
     *
     * @param account      账户
     * @param num          数量
     * @param transferType 添加或扣除
     */
    @Override
    public void platUpdateActives(String account, Integer num, TransferType transferType) {
        if (org.apache.commons.lang.StringUtils.isEmpty(account) || num == null) {
            throw new UserJlbException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        User user = userDAO.loadByAccount(account);
        if (user == null) {
            throw new UserJlbException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", user.getCode());
        if (transferType == TransferType.In) {
            UserActive userActive = userActiveDAO.load(param);
            if (userActive == null) {
                userActiveDAO.insert(new UserActive(user.getCode(), num));
            } else {
                userActive.setActive(userActive.getActive() + num);
                userActiveDAO.update(userActive);
            }
            activeRecordDAO.insert(new ActiveRecord(String.valueOf(num), user.getCode(), "平台为账户[" + user.getAccount() + "]充值[" + num + "]个激活币"));


        } else if (transferType == TransferType.Out) {
            UserActive userActive = userActiveDAO.load(param);
            if (userActive == null) {
                userActiveDAO.insert(new UserActive(user.getCode(), 0));
                throw new UserActiveException(BaseException.BaseExceptionEnum.Active_Not_Enough_Error);
            } else {
                userActive.setActive(userActive.getActive() - num);
                userActiveDAO.update(userActive);
            }
            activeRecordDAO.insert(new ActiveRecord(String.valueOf(num*(-1)), user.getCode(), "平台扣除账户[" + user.getAccount() + "]的[" + num + "]个激活币"));

        }
    }

    @Override
    public int sum() {
        return userActiveDAO.sum();
    }

}
